package com.redis.data.seckillshopping.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 哲思小辉
 * @version 1.0.0
 * @ClassName JsonUtil.java
 * @Description fastJson工具类
 * @Param
 * @createTime 2020年03月25日 16:58:00
 */
@Slf4j
public class JsonUtil {
    private JsonUtil() {
    }

    /**
     * 转成json 字符串 （不保留格式，换行、空格都会去掉）
     *
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof String) {
            return String.valueOf(JSON.parse(object.toString()));
        }
        return JSON.toJSONString(object);
    }

    /**
     * 转成json 字符串(保留格式)
     *
     * @param object
     * @return
     */
    public static String toJsonString(Object object) {
        if (object == null) {
            return null;
        }
        return String.valueOf(JSON.parse(JSON.toJSONString(object)));
    }

    /**
     * 转成Map<String,Object>
     *
     * @param object
     * @return
     */
    public static Map<String, Object> toMap(Object object) {
        String text = toJson(object);
        Map<String, Object> map = null;
        if (text != null) {
            map = JSON.parseObject(text, Map.class);
        }
        return map;
    }

    /**
     * 转成Map<String,String>
     *
     * @param object
     * @return
     */
    public static Map<String, String> toMapString(Object object) {
        String text = toJson(object);
        Map<String, String> map = null;
        if (text != null) {
            map = JSON.parseObject(text, new TypeReference<Map<String, String>>() {
            });
        }
        return map;
    }

    /**
     * 转成Map<String,T>
     *
     * @param object
     * @return
     */
    public static <T> Map<String, T> toMap(Object object, Class<T> clzz) {
        String text = toJson(object);
        Map<String, T> map = null;
        if (text != null) {
            map = JSON.parseObject(text, new ParameterizedTypeMap(clzz));
        }
        return map;
    }


    /**
     * 转成List <String>
     *
     * @param object
     * @return
     */
    public static List toList(Object object) {
        String text = toJson(object);
        List list = null;
        if (text != null) {
            list = JSON.parseObject(text, List.class);
        }
        return list;
    }

    /**
     * 转成List<T>
     *
     * @param object
     * @param clzz
     * @param <T>
     * @return
     */
    public static <T> List<T> toList(Object object, Class<T> clzz) {
        String text = toJson(object);
        return JSON.parseArray(text, clzz);
    }

    /**
     * 转成JavaBean
     *
     * @param object
     * @param clzz
     * @param <T>
     * @return
     */
    public static <T> T toJavaBean(Object object, Class<T> clzz) {
        String text = toJson(object);
        T t = null;
        if (text != null) {
            t = JSON.parseObject(text, clzz);
        }
        return t;
    }

    /**
     * 转成Set
     *
     * @param object
     * @return
     */
    public static Set toSet(Object object) {
        String text = toJson(object);
        Set set = null;
        if (text != null) {
            set = JSON.parseObject(text, Set.class);
        }
        return set;
    }

    /**
     * 转成Set<T>
     *
     * @param object
     * @return
     */
    public static <T> Set<T> toSet(Object object, Class<T> clzz) {
        String text = toJson(object);
        Set<T> set = null;
        if (text != null) {
            set = JSON.parseObject(text, new ParameterizedTypeSet(clzz));
        }
        return set;
    }


    private static class ParameterizedTypeSet implements ParameterizedType {
        Class clazz;

        public ParameterizedTypeSet(Class clz) {
            clazz = clz;
        }

        @Override
        public Type[] getActualTypeArguments() {
            return new Type[]{clazz};
        }

        @Override
        public Type getRawType() {
            return Set.class;
        }

        @Override
        public Type getOwnerType() {
            return null;
        }
    }


    private static class ParameterizedTypeMap implements ParameterizedType {
        Class clazz;

        public ParameterizedTypeMap(Class clz) {
            clazz = clz;
        }

        @Override
        public Type[] getActualTypeArguments() {
            return new Type[]{String.class, clazz};
        }

        @Override
        public Type getRawType() {
            return Map.class;
        }

        @Override
        public Type getOwnerType() {
            return null;
        }
    }


}
